// 自定画板类
import 'dart:math';

import 'package:flutter/material.dart';

class MyCustomPainter extends CustomPainter {
  final double _animationValue;
  MyCustomPainter(this._animationValue);

  @override
  void paint(Canvas canvas, Size size) {
    var paint1 = Paint()
      ..color = Colors.red                // 画笔颜色
      ..style = PaintingStyle.stroke    // 画笔样式：填充
      ..strokeWidth = 5.0                // 画笔粗细
      ..strokeCap = StrokeCap.round;    // 末端样式

    double drawLeftLineFraction,drawRightLineFraction;
    if(_animationValue<0.5){
      drawLeftLineFraction = _animationValue/0.5;
      drawRightLineFraction=0.0;
    }else{
      drawLeftLineFraction = 1.0;
      drawRightLineFraction = (_animationValue-0.5)/0.5;
    }
    canvas.drawLine(const Offset(0,0), Offset(size.width*drawLeftLineFraction,size.height*drawLeftLineFraction), paint1);
    canvas.drawLine(Offset(size.width,0), Offset(size.width-size.width*drawRightLineFraction,size.height*drawRightLineFraction), paint1);

    var rect = Rect.fromCircle(center: Offset(size.width/2,size.height/2), radius: sqrt(pow(size.width,2)+pow(size.height,2))/2);
    canvas.drawArc(rect, -pi/2, pi*2*_animationValue, false, paint1);

    // canvas.drawCircle(Offset(size.width/2,size.height/2), sqrt(pow(size.width,2)+pow(size.height,2))/2, paint1);

    // var path = Path();
    // path.moveTo(0, size.height / 2); // 定义路径的起始坐标，从当前坐标连接到给定坐标
    // path.lineTo(size.width / 2, size.height * 5 / 6);
    // path.lineTo(size.width, 0);    // 继续连接路径
    // path.close();                    // 闭合路径
    // canvas.drawPath(path, paint1);
    //
    // paint1.color = Colors.green;
    // paint1.style = PaintingStyle.fill;
    // paint1.strokeWidth  = 5.0;
    // canvas.drawPath(path, paint1);
  }

  @override
  bool shouldRepaint(MyCustomPainter oldDelegate) {
    return oldDelegate._animationValue!=_animationValue;
  }
}